<?php
/**
 * PollingPlan.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-09-06 16:49:50
 * @modified    2023-09-06 16:49:50
 *
 */

namespace App\Models;

use App\Models\PollingPlan\History;
use App\Models\PollingPlan\Log;
use App\Repositories\PollingPlanRepo;
use Carbon\Carbon;

class PollingPlan extends BaseModel
{
    protected $table = 'polling_plan';
    protected $primaryKey = 'id';

    public const CREATED_AT = 'create_time';
    public const UPDATED_AT = 'update_time';

    protected $fillable = [
        'number',
        'name',
        'status',
        'plan_start_at',
        'plan_end_at',
        'circle_unit',
        'circle_count',
        'plan_at',
        'create_user_id',
        'create_user_name',
        'polling_user_id',
        'polling_user_name',
        'polling_customer_id',
        'polling_customer_name',
        'polling_customer_user_id',
        'polling_customer_user_name',
        'polling_customer_user_telephone',
        'polling_customer_address',
        'polling_description',
        'mark',
    ];

    protected $appends = [
        'status_text',
        'circle_unit_text',
        'btn_delete',
        'btn_pause',
        'btn_start'
    ];

    /**
     * 删除按钮
     *  状态: 未开始, 时间: 当前时间 < 计划开始时间
     *  状态: 进行中, 时间: 计划开始时间 < 当前时间 < 计划结束时间
     *  状态: 暂停, 时间: 计划开始时间 < 当前时间 < 计划结束时间
     *  状态: 结束, 时间: 计划结束时间 < 当前时间
     * @return bool
     */
    public function getBtnDeleteAttribute()
    {
        $status = $this->status;
        $now = Carbon::parse()->toDateTimeString();
        $planStartAt = Carbon::parse($this->plan_start_at)->toDateTimeString();
        $planEndAt = Carbon::parse($this->plan_end_at)->toDateTimeString();
        if ($status == PollingPlanRepo::STATUS_CREATE && $now < $planStartAt) {
            return true;
        }
        if ($status == PollingPlanRepo::STATUS_START && ($planStartAt < $now && $now < $planEndAt)) {
            return true;
        }
        if ($status == PollingPlanRepo::STATUS_PAUSE && ($planStartAt < $now && $now < $planEndAt)) {
            return true;
        }
        if ($status == PollingPlanRepo::STATUS_COMPLETE && $planEndAt < $now) {
            return true;
        }
        return false;
    }

    /**
     * 暂停按钮
     *  状态: 进行中 时间: 计划开始时间 < 当前时间 < 计划结束时间
     * @return bool
     */
    public function getBtnPauseAttribute()
    {
        $status = $this->status;
        $now = Carbon::parse()->toDateTimeString();
        $planStartAt = Carbon::parse($this->plan_start_at)->toDateTimeString();
        $planEndAt = Carbon::parse($this->plan_end_at)->toDateTimeString();

        if ($status == PollingPlanRepo::STATUS_START && ($planStartAt < $now && $now < $planEndAt)) {
            return true;
        }
        return false;
    }

    /**
     * 开始按钮
     *  状态: 暂停 时间: 计划开始时间 < 当前时间 < 计划结束时间
     * @return bool
     */
    public function getBtnStartAttribute()
    {
        $status = $this->status;
        $now = Carbon::parse()->toDateTimeString();
        $planStartAt = Carbon::parse($this->plan_start_at)->toDateTimeString();
        $planEndAt = Carbon::parse($this->plan_end_at)->toDateTimeString();

        if ($status == PollingPlanRepo::STATUS_PAUSE && ($planStartAt < $now && $now < $planEndAt)) {
            return true;
        }
        return false;
    }

    public function getStatusTextAttribute()
    {
        return PollingPlanRepo::getStatusText($this->status);
    }

    public function getCircleUnitTextAttribute()
    {
        return PollingPlanRepo::getCircleUnitText($this->circle_unit);
    }

    public function scopeByNumber($query, $number)
    {
        return $query->where('number', $number);
    }

    public function histories()
    {
        return $this->hasMany(History::class);
    }

    public function logs()
    {
        return $this->hasMany(Log::class);
    }
}